﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VoteSystem.Models;

namespace VoteSystem.Services
{
    public class ConfigService
    {
        public VoteConfig GetActiveVoteConfig()
        {
            using (VoteSystemContext db = new VoteSystemContext())
            {
                return db.VoteConfigs.First(v => v.Active); 
            }
        }

        public OpResult ChangeVoteConfigDateTime(DateTime startTime1,DateTime endTime1,
                                                 DateTime startTime2,DateTime endTime2,
                                                 DateTime startTime3,DateTime endTime3)
        {
            OpResult result = new OpResult();

            try
            {
                using (VoteSystemContext db = new VoteSystemContext())
                {
                    var config = db.VoteConfigs.First(v => v.Active);
                    config.StartTime1 = startTime1;
                    config.StartTime2 = startTime2;
                    config.StartTime3 = startTime3;
                    config.EndTime1 = endTime1;
                    config.EndTime2 = endTime2;
                    config.EndTime3 = endTime3;
                    db.SaveChanges();
                    result.IsSuccess = true;
                }

            }
            catch (Exception ex)
            {
                result.ErrorMessage = "修改失败！";
                result.IsSuccess = false;
                LogHelper.Error(ex.Message);
                LogHelper.Error(ex.StackTrace);
                
            } return result;
        }
    }
}